package com.suncp.controller;

import com.suncp.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author suncp
 * @date 2020/12/12 14:36
 */
@RestController
public class AjaxController {

    @RequestMapping("/t1")
    public String test1() {
        return "hello";
    }

    @RequestMapping("/a1")
    public void a1(String name, HttpServletResponse response) throws IOException {
        System.out.println("a1:param=" + name);
        if ("scp".equals(name)) {
            response.getWriter().print("true");
        } else {
            response.getWriter().print("false");
        }
    }

    @RequestMapping("/a2")
    public List<User> a2() {
        List<User> users = new ArrayList<User>();

        //添加数据
        users.add(new User("suncp", 24, "男"));
        users.add(new User("张三", 12, "男"));
        users.add(new User("李华", 30, "女"));

        return users;
    }


    @RequestMapping("/a3")
    public String a3(String name,String pwd) {
        String msg = "";
        if (name != null) {
            // name 这些数据应该再数据库中
            if ("admin".equals(name)) {
                msg = "ok";
            } else {
                msg = "用户名有误!";
            }
        }
        if (pwd != null) {
            if ("admin".equals(pwd)) {
                msg = "ok";
            } else {
                msg = "密码有误!";
            }
        }
        return msg;
    }

}
